/**
 * [21] 合并两个有序链表
 *
 * @format
 * @lc app=leetcode.cn id=21 lang=javascript
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} list1
 * @param {ListNode} list2
 * @return {ListNode}
 */
var mergeTwoLists = function (list1, list2) {
    const list = new ListNode(-1)
    let [i1, i2] = [0, 0]
    const [L1, L2] = [list1.length, list2.length]
    while (i1 < L1 || i2 < L2) {
        let [n1, n2] = [list1[i1], list2[i2]]
        if (n1 < n2 || i2 == L2) {
            list.next = n1
            i1++
        } else {
            list.next = n2
            i2++
        }
    }
    return list
}
// @lc code=end
